package com.smart.campus.web.controller.student;

import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.config.Global;
import com.smart.campus.common.constant.ShiroConstants;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.common.utils.ServletUtils;
import com.smart.campus.school.domain.Msg;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.domain.request.StudentLoginRequest;
import com.smart.campus.school.service.IMsgService;
import com.smart.campus.school.service.IStudentService;
import com.smart.campus.web.utils.StudentUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学生信息Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-18
 */
@Controller
@RequestMapping("/student")
public class StudentLoginController extends BaseController
{
    private String prefix = "student";

    @Autowired
    private IMsgService msgService;

    @Autowired
    private IStudentService studentService;
    @Log(title = "学生登录", businessType = BusinessType.GRANT)
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(StudentLoginRequest request,HttpServletRequest httpServletRequest)
    {

        // 验证码校验
        if (!StringUtils.isEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA)))
        {
            return AjaxResult.error("验证码错误");
        }
        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(request.getUsername()) || StringUtils.isEmpty(request.getPassword()))
        {
            return AjaxResult.error("学号或密码为空");
        }
        Student student= studentService.selectStudentByLogin(request.getUsername(),request.getPassword());
        // 用户名或密码为空 错误
        if (student ==  null)
        {
            return AjaxResult.error("学号或密码错误");
        }
        httpServletRequest.getSession().setAttribute("loginStudent",student);
        return AjaxResult.success(student);
    }

    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response)
    {
        // 如果是Ajax请求，返回Json字符串。
        if (ServletUtils.isAjaxRequest(request))
        {
            return ServletUtils.renderString(response, "{\"code\":\"1\",\"msg\":\"未登录或登录超时。请重新登录\"}");
        }

        return prefix+"/login";
    }

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap,HttpServletRequest request)
    {
        // 取身份信息
        Student student = (Student) request.getSession().getAttribute("loginStudent");

        //加载消息中心
        Msg msg = new Msg();
        msg.setUserId(StudentUtil.getStudentId());
        msg.setUserType(2);
        msg.setStatus(0);
        List<Msg> msgList = msgService.selectMsgList(msg);
        mmap.put("schoolName", "智慧校园平台");

        mmap.put("student", student);
        mmap.put("copyrightYear", Global.getCopyrightYear());
        mmap.put("msgCount", msgList.size());
        return prefix+"/index";
    }
    
}
